package com.fone.player.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;

import com.fone.player.entity.OfflineCacheFragment;
import com.fone.player.entity.PlayRecord;
import com.fone.player.util.L;

/**
 * 
 * 播放记录表适配器
 * 
 * @author wangyang
 * @since 2014年4月29日
 */
class PlayRecordDataBaseAdapter implements IFoneDatabase {
	private static final String TAG = PlayRecordDataBaseAdapter.class
			.getSimpleName();
	private static final PlayRecordDataBaseAdapter mInstance = new PlayRecordDataBaseAdapter();
	private OperateDataBaseTemplate mGeneralDataBaseTemplate = OperateDataBaseTemplate
			.getInstance();

	private PlayRecordDataBaseAdapter() {

	}

	static PlayRecordDataBaseAdapter getInstance() {
		return mInstance;
	}

	public void exeSQL(String sql) {
		mGeneralDataBaseTemplate.open();
		mGeneralDataBaseTemplate.execSQL(sql);
		mGeneralDataBaseTemplate.close();
	}

	/**
	 * 
	 * 添加播放记录
	 * 
	 * @param playRecord
	 *            播放记录对象
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * 
	 */
	public int addPlayRecord(PlayRecord playRecord) {
		L.v(TAG, "addPlayRecord" + playRecord.toString());
		synchronized (TAG) {
			StringBuilder selectSql = new StringBuilder();
			selectSql.append("select * from ");
			selectSql.append(TB_PLAY_RECORD);
			selectSql.append(" where ");
			selectSql.append(PLAY_RECORD_TYPE);
			selectSql.append("=");
			selectSql.append(playRecord.getPlayRecordType());
			selectSql.append(" and ");
			switch (playRecord.getPlayRecordType()) {
			case PlayRecord.RECORD_PLAYER_LOCAL:// 本地
			case PlayRecord.RECORD_WEB_NETWORK:// 网络视频

				selectSql.append(PLAY_RECORD_PLAY_URL);
				selectSql.append("='");
				selectSql.append(playRecord.getPlayRecordPlayUrl());
				selectSql.append("'");
				break;
			case PlayRecord.RECORD_PLAYER_NETWORK:// 在线视频
			case PlayRecord.RECORD_PLAYER_CACHE:// 边下边播
				// 判断是否是VIP
				if (playRecord.getPlayRecordIsVIP()) {
					selectSql.append(PLAY_RECORD_VIDEO_ID);
					selectSql.append("=");
					selectSql.append(playRecord.getPlayRecordVideoID());
				} else {
					selectSql.append(PLAY_RECORD_CID);
					selectSql.append("=");
					selectSql.append(playRecord.getPlayRecordCID());
				}
				break;
			case PlayRecord.RECORD_WEB_SOURCE:// 源网页播放
				selectSql.append(PLAY_RECORD_CID);
				selectSql.append("=");
				selectSql.append(playRecord.getPlayRecordCID());
				break;
			default:
				break;
			}

			try {
				mGeneralDataBaseTemplate.open();
				Cursor cursor = mGeneralDataBaseTemplate.select(selectSql
						.toString());
				List<PlayRecord> updatePlayRecordList = new ArrayList<PlayRecord>();

				List<PlayRecord> addPlayRecordList = new ArrayList<PlayRecord>();

				if (cursor != null && cursor.getCount() > 0) {
					updatePlayRecordList.add(playRecord);
					// 更新
					cursor.close();
				} else {
					// 添加
					addPlayRecordList.add(playRecord);

				}
				mGeneralDataBaseTemplate.close();

				if (updatePlayRecordList.size() > 0) {
					updatePlayRecordList(updatePlayRecordList);
				}
				if (addPlayRecordList.size() > 0) {
					addPlayRecordList(addPlayRecordList);
				}

			} catch (Exception e) {
				e.printStackTrace();
				L.v(TAG, e.getMessage());
				return -1;
			} finally {

			}
			return 1;
		}
	}

	/**
	 * 
	 * 添加播放记录集合
	 * 
	 * @param playRecordList
	 *            播放记录对象
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * 
	 */
	public int addPlayRecordList(List<PlayRecord> playRecordList) {
		synchronized (TAG) {

			// 创建sql语句
			StringBuilder sql = new StringBuilder();
			sql.append("insert into ").append(TB_PLAY_RECORD).append(" (");
			sql.append(PLAY_RECORD_CID).append(",");
			sql.append(PLAY_RECORD_CCID).append(",");
			sql.append(PLAY_RECORD_CLID).append(",");
			sql.append(PLAY_RECORD_VIDEO_ID).append(",");
			sql.append(PLAY_RECORD_TYPE).append(",");
			sql.append(PLAY_RECORD_CONTENT_TYPE).append(",");
			sql.append(PLAY_RECORD_NAME).append(",");
			sql.append(PLAY_RECORD_IMAGE_URL).append(",");
			sql.append(PLAY_RECORD_PLAY_URL).append(",");
			sql.append(PLAY_RECORD_ALREADY_PLAY_TIME).append(",");
			sql.append(PLAY_RECORD_TOTAL_TIME).append(",");
			sql.append(PLAY_RECORD_CTEATE_TIME).append(",");
			sql.append(PLAY_RECORD_IS_VIP).append(",");
			sql.append(PLAY_RECORD_DESCRIPTION).append(",");
			sql.append(PLAY_RECORD_REMOVE_URL).append(",");
			sql.append(PLAY_RECORD_FAVOURITE_URL).append(",");
			sql.append(PLAY_RECORD_DETAIL_URL).append(",");
			sql.append(PLAY_RECORD_SHARE_URL).append(",");
			sql.append(PLAY_RECORD_SYNCHRONZIE_TYPE).append(",");
			sql.append(PLAY_RECORD_EPISODE_UPDATE_INFO).append(",");
			sql.append(PLAY_RECORD_EXTERNAL_URL).append(",");
			sql.append(PLAY_RECORD_IS_SHOW_PLAY_BUTTON);
			sql.append(") values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

			try {
				mGeneralDataBaseTemplate.open();
				mGeneralDataBaseTemplate.beginTransaction();

				SQLiteStatement fileStatement = mGeneralDataBaseTemplate
						.getSQLiteStatement(sql.toString());

				for (PlayRecord playRecord : playRecordList) {
					fileStatement.bindLong(1, playRecord.getPlayRecordCID());
					fileStatement.bindLong(2, playRecord.getPlayRecordCCID());
					fileStatement.bindLong(3, playRecord.getPlayRecordCLID());
					fileStatement
							.bindLong(4, playRecord.getPlayRecordVideoID());
					fileStatement.bindLong(5, playRecord.getPlayRecordType());
					fileStatement.bindLong(6,
							playRecord.getPlayRecordContentType());
					fileStatement.bindString(7, playRecord.getPlayRecordName());
					fileStatement.bindString(8,
							playRecord.getPlayRecordImageUrl());
					fileStatement.bindString(9,
							playRecord.getPlayRecordPlayUrl());
					fileStatement.bindLong(10,
							playRecord.getPlayRecordAlreadyPlayTime());
					fileStatement.bindLong(11,
							playRecord.getPlayRecordTotalTime());
					fileStatement.bindLong(12,
							playRecord.getPlayRecordCreateTime());
					fileStatement.bindLong(13,
							playRecord.getPlayRecordIsVIP() ? 1 : 0);
					fileStatement.bindString(14,
							playRecord.getPlayRecordDescription());
					fileStatement.bindString(15,
							playRecord.getPlayRecordRemoveUrl());
					fileStatement.bindString(16,
							playRecord.getPlayRecordFavouriteUrl());
					fileStatement.bindString(17,
							playRecord.getPlayRecordDetailUrl());
					fileStatement.bindString(18,
							playRecord.getPlayRecordShareUrl());
					fileStatement.bindLong(19,
							playRecord.getPlayRecordSynchronzieType());
					fileStatement.bindString(20,
							playRecord.getPlayRecordEpisodeUpdateInfo());
					fileStatement.bindString(21,
							playRecord.getPlayRecordExternalUrl());
					fileStatement.bindLong(22,
							playRecord.getPlayRecordIsShowPlayButton() ? 1 : 0);
					mGeneralDataBaseTemplate.insert(fileStatement);
				}

				mGeneralDataBaseTemplate.setTransactionSuccessful();

			} catch (Exception e) {
				e.printStackTrace();
				if (e != null) {
					L.v(TAG, e.getMessage());
				}
				return -1;
			} finally {
				mGeneralDataBaseTemplate.endTransaction();
				mGeneralDataBaseTemplate.close();
			}
			return 1;
		}
	}

	/**
	 * 
	 * 更新播放记录集合
	 * 
	 * @param playRecord
	 *            播放记录对象
	 * @return int 更新结果<br>
	 *         1 更新成功 <br>
	 *         -1 更新异常
	 * 
	 */
	public int updatePlayRecordList(List<PlayRecord> playRecordList) {

		synchronized (TAG) {
			try {
				mGeneralDataBaseTemplate.open();
				int result = 0;
				for (PlayRecord playRecord : playRecordList) {
					ContentValues contentValues = new ContentValues();
					contentValues.put(PLAY_RECORD_CID,
							playRecord.getPlayRecordCID());
					contentValues.put(PLAY_RECORD_CCID,
							playRecord.getPlayRecordCCID());
					contentValues.put(PLAY_RECORD_CLID,
							playRecord.getPlayRecordCLID());
					contentValues.put(PLAY_RECORD_VIDEO_ID,
							playRecord.getPlayRecordVideoID());
					contentValues.put(PLAY_RECORD_TYPE,
							playRecord.getPlayRecordType());
					contentValues.put(PLAY_RECORD_CONTENT_TYPE,
							playRecord.getPlayRecordContentType());
					contentValues.put(PLAY_RECORD_NAME,
							playRecord.getPlayRecordName());

					if (!TextUtils.isEmpty(playRecord.getPlayRecordImageUrl())) {
						contentValues.put(PLAY_RECORD_IMAGE_URL,
								playRecord.getPlayRecordImageUrl());
					}
					contentValues.put(PLAY_RECORD_PLAY_URL,
							playRecord.getPlayRecordPlayUrl());
					contentValues.put(PLAY_RECORD_ALREADY_PLAY_TIME,
							playRecord.getPlayRecordAlreadyPlayTime());
					contentValues.put(PLAY_RECORD_CTEATE_TIME,
							playRecord.getPlayRecordCreateTime());
					contentValues.put(PLAY_RECORD_IS_VIP,
							playRecord.getPlayRecordIsVIP());
					contentValues.put(PLAY_RECORD_DESCRIPTION,
							playRecord.getPlayRecordDescription());
					contentValues.put(PLAY_RECORD_REMOVE_URL,
							playRecord.getPlayRecordRemoveUrl());
					contentValues.put(PLAY_RECORD_FAVOURITE_URL,
							playRecord.getPlayRecordFavouriteUrl());
					contentValues.put(PLAY_RECORD_DETAIL_URL,
							playRecord.getPlayRecordDetailUrl());
					contentValues.put(PLAY_RECORD_SHARE_URL,
							playRecord.getPlayRecordShareUrl());
					contentValues.put(PLAY_RECORD_SYNCHRONZIE_TYPE,
							playRecord.getPlayRecordSynchronzieType());
					contentValues.put(PLAY_RECORD_EPISODE_UPDATE_INFO,
							playRecord.getPlayRecordEpisodeUpdateInfo());
					contentValues.put(PLAY_RECORD_EXTERNAL_URL,
							playRecord.getPlayRecordExternalUrl());
					contentValues.put(PLAY_RECORD_IS_SHOW_PLAY_BUTTON,
							playRecord.getPlayRecordIsShowPlayButton());
					StringBuilder where = new StringBuilder();
					where.append(PLAY_RECORD_TYPE);
					where.append("=");
					where.append(playRecord.getPlayRecordType());
					where.append(" and ");
					switch (playRecord.getPlayRecordType()) {
					case PlayRecord.RECORD_PLAYER_LOCAL:
					case PlayRecord.RECORD_WEB_NETWORK:
						where.append(PLAY_RECORD_PLAY_URL);
						where.append("=?");
						result = mGeneralDataBaseTemplate.update(
								TB_PLAY_RECORD, contentValues,
								where.toString(), new String[] { playRecord
										.getPlayRecordPlayUrl() });
						L.v(TAG, "updatePlayRecordList",
								"result=" + result + " PLAY_RECORD_PLAY_URL="
										+ playRecord.getPlayRecordPlayUrl());
						break;
					case PlayRecord.RECORD_PLAYER_NETWORK:
					case PlayRecord.RECORD_PLAYER_CACHE:// 边下边播
						// 判断是否是VIP
						if (playRecord.getPlayRecordIsVIP()) {
							where.append(PLAY_RECORD_VIDEO_ID);
							where.append("=?");
							result = mGeneralDataBaseTemplate.update(
									TB_PLAY_RECORD, contentValues, where
											.toString(), new String[] { String
											.valueOf(playRecord
													.getPlayRecordVideoID()) });
							L.v(TAG, "updatePlayRecordList",
									"result=" + result
											+ " PLAY_RECORD_VIDEO_ID="
											+ playRecord.getPlayRecordVideoID());
						} else {
							where.append(PLAY_RECORD_CID);
							where.append("=?");
							result = mGeneralDataBaseTemplate.update(
									TB_PLAY_RECORD, contentValues, where
											.toString(), new String[] { String
											.valueOf(playRecord
													.getPlayRecordCID()) });
							L.v(TAG, "updatePlayRecordList",
									"result=" + result + " PLAY_RECORD_CID="
											+ playRecord.getPlayRecordCID());
						}
						break;
					case PlayRecord.RECORD_WEB_SOURCE:
						where.append(PLAY_RECORD_CID);
						where.append("=?");
						result = mGeneralDataBaseTemplate.update(
								TB_PLAY_RECORD, contentValues,
								where.toString(),
								new String[] { String.valueOf(playRecord
										.getPlayRecordCID()) });
						L.v(TAG, "updatePlayRecordList",
								"result=" + result + " PLAY_RECORD_CID="
										+ playRecord.getPlayRecordCID());
						break;
					default:
						break;
					}

				}
			} catch (Exception e) {
				e.printStackTrace();
				L.v(TAG, e.getMessage());
				return -1;
			} finally {
				mGeneralDataBaseTemplate.close();
			}
			return 1;
		}
	}

	/**
	 * 
	 * 根据保留记录的数量删除多余的记录 <br>
	 * 同步方法
	 * 
	 * @param persistNum
	 *            保留数目
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deletePlayRecordByPersistNum(int persistNum) {
		try {

			// 删除本地数据库
			StringBuilder sql = new StringBuilder();
			sql.append("delete from ").append(TB_PLAY_RECORD);
			sql.append(" where ");
			sql.append(PLAY_RECORD_VIDEO_ID).append(" not in ");
			sql.append("(select ");
			sql.append(PLAY_RECORD_VIDEO_ID);
			sql.append("from");
			sql.append(TB_PLAY_RECORD);
			sql.append("where ");
			sql.append(PLAY_RECORD_TYPE);
			sql.append("=");
			sql.append(PlayRecord.RECORD_PLAYER_LOCAL);
			sql.append(" or ");
			sql.append(PLAY_RECORD_TYPE);
			sql.append("=");
			sql.append(PlayRecord.RECORD_PLAYER_NETWORK);
			sql.append(" order by ");
			sql.append(PLAY_RECORD_CTEATE_TIME);
			sql.append(" desc limit 0,");
			sql.append(persistNum);
			sql.append(")");
			mGeneralDataBaseTemplate.open();
			mGeneralDataBaseTemplate.delete(sql.toString());

			return 1;

		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "deletePlayRecordByPersistNum", e.getMessage());
			return -1;
		} finally {
			mGeneralDataBaseTemplate.open();
		}
	}

	/**
	 * 
	 * 根据类型和连接删除播放记录 <br>
	 * 同步方法
	 * 
	 * @param playRecord
	 *            播放记录对象
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deletePlayRecord(PlayRecord playRecord) {
		try {

			// 删除本地数据库
			StringBuilder sql = new StringBuilder();
			sql.append("delete from ").append(TB_PLAY_RECORD);
			sql.append(" where ");
			sql.append(PLAY_RECORD_TYPE).append("=");
			sql.append(playRecord.getPlayRecordType());
			sql.append(" and ");
			sql.append(PLAY_RECORD_PLAY_URL);
			sql.append("='");
			sql.append(playRecord.getPlayRecordPlayUrl());
			sql.append("';");
			mGeneralDataBaseTemplate.open();
			mGeneralDataBaseTemplate.delete(sql.toString());

			return 1;

		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "deletePlayRecordByPersistNum", e.getMessage());
			return -1;
		} finally {
			mGeneralDataBaseTemplate.close();
		}
	}

	/**
	 * 
	 * 根据播放记录类型删除指定的播放记录 <br>
	 * 同步方法
	 * 
	 * @param playRecordType
	 *            播放记录类型
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deletePlayRecord(int playRecordType) {
		try {
			int result = 0;
			mGeneralDataBaseTemplate.open();
			// 删除本地数据库
			StringBuilder where = new StringBuilder();
			switch (playRecordType) {
			case PlayRecord.RECORD_WEB_ALL:
				where.append(PLAY_RECORD_TYPE).append("=?");
				where.append(" or ");
				where.append(PLAY_RECORD_TYPE).append("=?");
				result = mGeneralDataBaseTemplate
						.delete(TB_PLAY_RECORD,
								where.toString(),
								new String[] {
										String.valueOf(PlayRecord.RECORD_PLAYER_NETWORK),
										String.valueOf(PlayRecord.RECORD_WEB_SOURCE) });
				break;
			default:
				where.append(PLAY_RECORD_TYPE).append("=?");
				result = mGeneralDataBaseTemplate.delete(TB_PLAY_RECORD,
						where.toString(),
						new String[] { String.valueOf(playRecordType) });
				break;
			}

			return result > 0 ? 1 : -1;

		} catch (Exception e) {
			e.printStackTrace();
			L.e(TAG, "deletePlayRecordByPersistNum", e.getMessage());
			return -1;
		} finally {
			mGeneralDataBaseTemplate.close();
		}
	}

	/**
	 * 
	 * 根据同步类型获得播放记录集合中的第一个记录(根据时间倒序)
	 * 
	 * @return PlayRecord 播放记录对象
	 */
	public PlayRecord getPlayRecordByPlayRecordSynchronzieType() {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		sql.append(PLAY_RECORD_SYNCHRONZIE_TYPE);
		sql.append("!=");
		sql.append(PlayRecord.PLAY_RECORD_SYNCHRONZIE_DELETE_WAIT);
		sql.append(" order by ");
		sql.append(PLAY_RECORD_CTEATE_TIME);
		sql.append(" desc");
		sql.append(" limit ");
		// 要显示多少条记录
		sql.append(1);
		sql.append(" offset ");
		// 跳过多少条记录
		sql.append(0);
		PlayRecord playRecord = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			while (cursor.moveToNext()) {
				playRecord = getPlayRecord(cursor);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		// 合并播放记录中的视频分片信息
		mergeOfflineCacheFragmentListByPlayRecord(playRecord);

		return playRecord;
	}

	/**
	 * 
	 * 获得播放记录集合
	 * 
	 * @return ArrayList<PlayRecord> 播放记录集合
	 */
	public ArrayList<PlayRecord> getPlayRecordList(int playRecordType) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		switch (playRecordType) {
		case PlayRecord.RECORD_WEB_ALL:
			sql.append(PLAY_RECORD_TYPE);
			sql.append("=");
			sql.append(PlayRecord.RECORD_WEB_SOURCE);
			sql.append(" or ");
			sql.append(PLAY_RECORD_TYPE);
			sql.append("=");
			sql.append(PlayRecord.RECORD_PLAYER_NETWORK);
			break;
		default:
			sql.append(PLAY_RECORD_TYPE);
			sql.append("=");
			sql.append(playRecordType);
			break;
		}

		ArrayList<PlayRecord> playRecordList = new ArrayList<PlayRecord>();
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}

			// 读取数据
			while (cursor.moveToNext()) {
				PlayRecord playRecord = getPlayRecord(cursor);
				playRecordList.add(playRecord);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		for (PlayRecord playRecord : playRecordList) {
			// 合并播放记录中的视频分片信息
			mergeOfflineCacheFragmentListByPlayRecord(playRecord);
		}
		return playRecordList;
	}

	/**
	 * 
	 * 根据播放地址获得播放记录
	 * 
	 * @param playUrl
	 *            播放地址
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getPlayRecordByPlayUrl(String playUrl) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		sql.append(PLAY_RECORD_PLAY_URL);
		sql.append("='");
		sql.append(playUrl);
		sql.append("'");
		PlayRecord playRecord = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}
			// 读取数据
			while (cursor.moveToNext()) {
				playRecord = getPlayRecord(cursor);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		// 合并播放记录中的视频分片信息
		mergeOfflineCacheFragmentListByPlayRecord(playRecord);

		return playRecord;
	}

	/**
	 * 
	 * 根据VideoID获得播放记录
	 * 
	 * @param videoID
	 *            片库ID
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getPlayRecordByVideoID(long videoID) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		sql.append(PLAY_RECORD_VIDEO_ID);
		sql.append("=");
		sql.append(videoID);
		PlayRecord playRecord = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}
			// 读取数据
			while (cursor.moveToNext()) {
				playRecord = getPlayRecord(cursor);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		// 合并播放记录中的视频分片信息
		mergeOfflineCacheFragmentListByPlayRecord(playRecord);

		return playRecord;
	}

	/**
	 * 
	 * 根据CID获得播放记录
	 * 
	 * @param CID
	 *            内容ID
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getPlayRecordByCID(long CID) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		sql.append(PLAY_RECORD_CID);
		sql.append("=");
		sql.append(CID);
		PlayRecord playRecord = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}
			// 读取数据
			while (cursor.moveToNext()) {
				playRecord = getPlayRecord(cursor);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		// 合并播放记录中的视频分片信息
		mergeOfflineCacheFragmentListByPlayRecord(playRecord);

		return playRecord;
	}

	/**
	 * 
	 * 根据VideoID获得最近的播放记录
	 * 
	 * @param VideoID
	 *            片库ID
	 * @param CCID
	 *            内容集合ID
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getRecentlyPlayRecordByID(long videoID, long CCID) {
		StringBuilder sql = new StringBuilder("select * from ");
		sql.append(TB_PLAY_RECORD);
		sql.append(" where ");
		sql.append(PLAY_RECORD_VIDEO_ID);
		sql.append("=");
		sql.append(videoID);
		sql.append(" and ");
		sql.append(PLAY_RECORD_CCID);
		sql.append("=");
		sql.append(CCID);
		sql.append(" order by ");
		sql.append(PLAY_RECORD_CTEATE_TIME);
		sql.append(" desc");
		sql.append(" limit ");
		// 要显示多少条记录
		sql.append(1);
		sql.append(" offset ");
		// 跳过多少条记录
		sql.append(0);
		PlayRecord playRecord = null;
		Cursor cursor = null;
		try {
			mGeneralDataBaseTemplate.open();
			cursor = mGeneralDataBaseTemplate.select(sql.toString());

			if (cursor == null) {
				return null;
			}
			// 读取数据
			while (cursor.moveToNext()) {
				playRecord = getPlayRecord(cursor);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			mGeneralDataBaseTemplate.close();
		}

		// 合并播放记录中的视频分片信息
		mergeOfflineCacheFragmentListByPlayRecord(playRecord);

		return playRecord;
	}

	private synchronized PlayRecord getPlayRecord(Cursor cursor) {

		PlayRecord playRecord = new PlayRecord();
		playRecord.setPlayRecordCID(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_CID)));
		playRecord.setPlayRecordCCID(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_CCID)));
		playRecord.setPlayRecordCLID(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_CLID)));
		playRecord.setPlayRecordVideoID(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_VIDEO_ID)));
		playRecord.setPlayRecordType(cursor.getInt(cursor
				.getColumnIndex(PLAY_RECORD_TYPE)));
		playRecord.setPlayRecordContentType(cursor.getInt(cursor
				.getColumnIndex(PLAY_RECORD_CONTENT_TYPE)));
		playRecord.setPlayRecordName(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_NAME)));
		playRecord.setPlayRecordDetailUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_DETAIL_URL)));
		playRecord.setPlayRecordFavouriteUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_FAVOURITE_URL)));
		playRecord.setPlayRecordRemoveUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_REMOVE_URL)));
		playRecord.setPlayRecordShareUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_SHARE_URL)));
		playRecord.setPlayRecordSynchronzieType(cursor.getInt(cursor
				.getColumnIndex(PLAY_RECORD_SYNCHRONZIE_TYPE)));
		playRecord.setPlayRecordDescription(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_DESCRIPTION)));
		playRecord.setPlayRecordImageUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_IMAGE_URL)));
		playRecord.setPlayRecordIsVIP(cursor.getInt(cursor
				.getColumnIndex(PLAY_RECORD_IS_VIP)) == 1);
		playRecord.setPlayRecordPlayUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_PLAY_URL)));
		playRecord.setPlayRecordEpisodeUpdateInfo(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_EPISODE_UPDATE_INFO)));
		playRecord.setPlayRecordAlreadyPlayTime(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_ALREADY_PLAY_TIME)));
		playRecord.setPlayRecordTotalTime(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_TOTAL_TIME)));
		playRecord.setPlayRecordCreateTime(cursor.getLong(cursor
				.getColumnIndex(PLAY_RECORD_CTEATE_TIME)));
		playRecord.setPlayRecordExternalUrl(cursor.getString(cursor
				.getColumnIndex(PLAY_RECORD_EXTERNAL_URL)));
		playRecord.setPlayRecordIsShowPlayButton(cursor.getInt(cursor
				.getColumnIndex(PLAY_RECORD_IS_SHOW_PLAY_BUTTON)) == 1);

		return playRecord;
	}

	/**
	 * 
	 * 合并播放记录中的视频分片信息
	 * 
	 * @param playRecord
	 * @return 参数X描述... （注意：请将参数、描述都对齐）
	 * @return void
	 * @throws
	 */
	private void mergeOfflineCacheFragmentListByPlayRecord(PlayRecord playRecord) {
		if (playRecord != null) {
			List<OfflineCacheFragment> offlineCacheFragmentList = OfflineCacheFragmentDataBaseAdapter
					.getInstance().getOfflineCacheFragmentListByCID(
							playRecord.getPlayRecordCID());
			if (offlineCacheFragmentList != null) {
				playRecord
						.setOfflineCacheFragmentList(offlineCacheFragmentList);
			}
		}
	}
}
